package com.jiwei.community.controller;

import com.jiwei.community.entity.Category;
import com.jiwei.community.enums.AppHttpCodeEnum;
import com.jiwei.community.exception.SystemException;
import com.jiwei.community.service.CommunityService;
import com.jiwei.community.utils.SecurityUtils;
import com.jiwei.community.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * @author 季伟
 * @date 2024/3/21
 */
@RestController
@RequestMapping("/community")
public class CommunityController {
    @Autowired
    CommunityService communityService;
    @GetMapping("/list")
    public ResponseResult getCommunityList(Integer pageNum, Integer pageSize,Long categoryId){
        return communityService.getCommunityList(pageNum,pageSize,categoryId);
    }
    @PostMapping("/join/{communityId}")
    public ResponseResult join(@PathVariable Long communityId){
        return communityService.join(SecurityUtils.getUserId(),communityId);
    }
    @GetMapping("/detail/{communityId}")
    public ResponseResult getCommunity(@PathVariable Long communityId){
        return communityService.getCommunityById(communityId);
    }
    @GetMapping("/isJoined/{communityId}")
    public ResponseResult isJoined(@PathVariable Long communityId){
        Long userId = -1L;
        try {
            userId = SecurityUtils.getUserId();
        }catch (Exception ignored){
        }
        return communityService.isJoined(userId,communityId);
    }
    @DeleteMapping("/join/{communityId}")
    public ResponseResult cancelFollow(@PathVariable Long communityId){
        return communityService.cancelJoin(SecurityUtils.getUserId(),communityId);
    }

    @GetMapping("/sign/isSigned")
    public ResponseResult isSigned(Long communityId){
        if (communityId == null){
            throw new SystemException(AppHttpCodeEnum.BAD_REQUEST,"communityId不能为空");
        }
        Long userId = null;
        try {
            userId = SecurityUtils.getUserId();
        }catch (SystemException ignored){

        }
        if (userId == null){
            Map<String,Object> map = new HashMap<>();
            map.put("isSigned",false);
            map.put("dur",0);
            return ResponseResult.okResult(map);
        }
        return communityService.isSigned(userId,communityId);
    }
    @PostMapping("/signIn")
    public ResponseResult signIn(Long communityId){
        if (communityId == null){
            throw new SystemException(AppHttpCodeEnum.BAD_REQUEST,"communityId不能为空");
        }
        Long userId = SecurityUtils.getUserId();

        return communityService.signIn(userId,communityId);
    }
    @GetMapping("/joined")
    public ResponseResult getJoinedCommunity(){
        Long userId = null;
        try {
            userId = SecurityUtils.getUserId();
        }catch (SystemException e){

        }
        if (userId==null){
            return ResponseResult.okResult(new LinkedList<>());
        }
        return communityService.getJoinedCommunity(userId);
    }


}
